<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>聊天</title>
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="../css/main.css" />
		<link href="../css/mui.imageviewer.css" rel="stylesheet" />
		<link rel="stylesheet" href="../js/qs/qs.common.css" />
		<style>
			html,
			body {
				height: 100%;
				margin: 0px;
				padding: 0px;
				overflow: hidden;
				-webkit-touch-callout: none;
				-webkit-user-select: none;
			}
			
			footer {
				position: fixed;
				width: 100%;
				height: 50px;
				min-height: 50px;
				border-top: solid 1px #bbb;
				left: 0px;
				bottom: 0px;
				overflow: hidden;
				padding: 0px 50px;
				background-color: #fafafa;
			}
			
			.footer-left {
				position: absolute;
				width: 50px;
				height: 50px;
				left: 0px;
				bottom: 0px;
				text-align: center;
				vertical-align: middle;
				line-height: 100%;
				padding: 12px 4px;
			}
			
			.footer-right {
				position: absolute;
				width: 50px;
				height: 50px;
				right: 0px;
				bottom: 0px;
				text-align: center;
				vertical-align: middle;
				line-height: 100%;
				padding: 12px 5px;
				display: inline-block;
			}
			
			.footer-center {
				height: 100%;
				padding: 5px 0px;
			}
			
			.footer-center [class*=input] {
				width: 100%;
				height: 100%;
				border-radius: 5px;
			}
			
			.footer-center .input-text {
				background: #fff;
				border: solid 1px #ddd;
				padding: 10px !important;
				font-size: 16px !important;
				line-height: 18px !important;
				font-family: verdana !important;
				overflow: hidden;
			}
			
			.footer-center .input-sound {
				background-color: #eee;
			}
			
			.mui-content {
				height: 100%;
				padding: 44px 0px 50px 0px;
				overflow: auto;
				background-color: #eaeaea;
			}
			
			#msg-list {
				height: 100%;
				overflow: auto;
				-webkit-overflow-scrolling: touch;
			}
			
			.msg-item {
				padding: 8px;
				clear: both;
			}
			
			.msg-item .mui-item-clear {
				clear: both;
			}
			
			.msg-item .msg-user {
				width: 38px;
				height: 38px;
				border: solid 1px #d3d3d3;
				display: inline-block;
				background: #fff;
				border-radius: 3px;
				vertical-align: top;
				text-align: center;
				float: left;
				padding: 3px;
				color: #ddd;
			}
			
			.msg-item .msg-user-img {
				width: 38px;
				height: 38px;
				display: inline-block;
				border-radius: 3px;
				vertical-align: top;
				text-align: center;
				float: left;
				color: #ddd;
			}
			
			.msg-item .msg-content {
				display: inline-block;
				border-radius: 5px;
				border: solid 1px #d3d3d3;
				background-color: #FFFFFF;
				color: #333;
				padding: 8px;
				vertical-align: top;
				font-size: 15px;
				position: relative;
				margin: 0px 8px;
				max-width: 75%;
				min-width: 35px;
				float: left;
			}
			
			.msg-item .msg-content .msg-content-inner {
				overflow-x: hidden;
			}
			
			.msg-item .msg-content .msg-content-arrow {
				position: absolute;
				border: solid 1px #d3d3d3;
				border-right: none;
				border-top: none;
				background-color: #FFFFFF;
				width: 10px;
				height: 10px;
				left: -5px;
				top: 12px;
				-webkit-transform: rotateZ(45deg);
				transform: rotateZ(45deg);
			}
			
			.msg-item-self .msg-user,
			.msg-item-self .msg-content {
				float: right;
			}
			
			.msg-item-self .msg-content .msg-content-arrow {
				left: auto;
				right: -5px;
				-webkit-transform: rotateZ(225deg);
				transform: rotateZ(225deg);
			}
			
			.msg-item-self .msg-content,
			.msg-item-self .msg-content .msg-content-arrow {
				background-color: #4CD964;
				color: #fff;
				border-color: #2AC845;
			}
			
			footer .mui-icon {
				color: #000;
			}
			
			footer .mui-icon:active {
				color: #007AFF !important;
			}
			
			footer .mui-icon-paperplane:before {
				content: "发送";
			}
			
			footer .mui-icon-paperplane {
				font-size: 16px;
				word-break: keep-all;
				line-height: 100%;
				padding-top: 6px;
				color: rgba(0, 135, 250, 1);
			}
			
			#msg-sound {
				-webkit-user-select: none !important;
				user-select: none !important;
			}
			
			.rprogress {
				position: absolute;
				left: 50%;
				top: 50%;
				width: 140px;
				height: 140px;
				margin-left: -70px;
				margin-top: -70px;
				background-image: url(../images/arecord.png);
				background-repeat: no-repeat;
				background-position: center center;
				background-size: 30px 30px;
				background-color: rgba(0, 0, 0, 0.7);
				border-radius: 5px;
				display: none;
				-webkit-transition: .15s;
			}
			
			.rschedule {
				background-color: rgba(0, 0, 0, 0);
				border: 5px solid rgba(0, 183, 229, 0.9);
				opacity: .9;
				border-left: 5px solid rgba(0, 0, 0, 0);
				border-right: 5px solid rgba(0, 0, 0, 0);
				border-radius: 50px;
				box-shadow: 0 0 15px #2187e7;
				width: 46px;
				height: 46px;
				position: absolute;
				left: 50%;
				top: 50%;
				margin-left: -23px;
				margin-top: -23px;
				-webkit-animation: spin 1s infinite linear;
				animation: spin 1s infinite linear;
			}
			
			.r-sigh {
				display: none;
				border-radius: 50px;
				box-shadow: 0 0 15px #2187e7;
				width: 46px;
				height: 46px;
				position: absolute;
				left: 50%;
				top: 50%;
				margin-left: -23px;
				margin-top: -23px;
				text-align: center;
				line-height: 46px;
				font-size: 40px;
				font-weight: bold;
				color: #2187e7;
			}
			
			.rprogress-sigh {
				background-image: none !important;
			}
			
			.rprogress-sigh .rschedule {
				display: none !important;
			}
			
			.rprogress-sigh .r-sigh {
				display: block !important;
			}
			
			.rsalert {
				font-size: 12px;
				color: #bbb;
				text-align: center;
				position: absolute;
				border-radius: 5px;
				width: 130px;
				margin: 5px 5px;
				padding: 5px;
				left: 0px;
				bottom: 0px;
			}
			
			@-webkit-keyframes spin {
				0% {
					-webkit-transform: rotate(0deg);
				}
				100% {
					-webkit-transform: rotate(360deg);
				}
			}
			
			@keyframes spin {
				0% {
					transform: rotate(0deg);
				}
				100% {
					transform: rotate(360deg);
				}
			}
			
			#h {
				background: #fff;
				border: solid 1px #ddd;
				padding: 10px !important;
				font-size: 16px !important;
				font-family: verdana !important;
				line-height: 18px !important;
				overflow: visible;
				position: absolute;
				left: -1000px;
				right: 0px;
				word-break: break-all;
				word-wrap: break-word;
			}
			
			.cancel {
				background-color: darkred;
			}
			
			.mui-preview-image.mui-fullscreen {
				position: fixed;
				z-index: 20;
				background-color: #000;
			}
			
			.mui-preview-header,
			.mui-preview-footer {
				position: absolute;
				width: 100%;
				left: 0;
				z-index: 10;
			}
			
			.mui-preview-header {
				height: 44px;
				top: 0;
			}
			
			.mui-preview-footer {
				height: 50px;
				bottom: 0px;
			}
			
			.mui-preview-header .mui-preview-indicator {
				display: block;
				line-height: 25px;
				color: #fff;
				text-align: center;
				margin: 15px auto 4;
				width: 70px;
				background-color: rgba(0, 0, 0, 0.4);
				border-radius: 12px;
				font-size: 16px;
			}
			
			.mui-preview-image {
				display: none;
				-webkit-animation-duration: 0.5s;
				animation-duration: 0.5s;
				-webkit-animation-fill-mode: both;
				animation-fill-mode: both;
			}
			
			.mui-preview-image.mui-preview-in {
				-webkit-animation-name: fadeIn;
				animation-name: fadeIn;
			}
			
			.mui-preview-image.mui-preview-out {
				background: none;
				-webkit-animation-name: fadeOut;
				animation-name: fadeOut;
			}
			
			.mui-preview-image.mui-preview-out .mui-preview-header,
			.mui-preview-image.mui-preview-out .mui-preview-footer {
				display: none;
			}
			
			.mui-zoom-scroller {
				position: absolute;
				display: -webkit-box;
				display: -webkit-flex;
				display: flex;
				-webkit-box-align: center;
				-webkit-align-items: center;
				align-items: center;
				-webkit-box-pack: center;
				-webkit-justify-content: center;
				justify-content: center;
				left: 0;
				right: 0;
				bottom: 0;
				top: 0;
				width: 100%;
				height: 100%;
				margin: 0;
				-webkit-backface-visibility: hidden;
			}
			
			.mui-zoom {
				-webkit-transform-style: preserve-3d;
				transform-style: preserve-3d;
			}
			
			.mui-slider .mui-slider-group .mui-slider-item img {
				width: auto;
				height: auto;
				max-width: 100%;
				max-height: 100%;
			}
			
			.mui-android-4-1 .mui-slider .mui-slider-group .mui-slider-item img {
				width: 100%;
			}
			
			.mui-android-4-1 .mui-slider.mui-preview-image .mui-slider-group .mui-slider-item {
				display: inline-table;
			}
			
			.mui-android-4-1 .mui-slider.mui-preview-image .mui-zoom-scroller img {
				display: table-cell;
				vertical-align: middle;
			}
			
			.mui-preview-loading {
				position: absolute;
				width: 100%;
				height: 100%;
				top: 0;
				left: 0;
				display: none;
			}
			
			.mui-preview-loading.mui-active {
				display: block;
			}
			
			.mui-preview-loading .mui-spinner-white {
				position: absolute;
				top: 50%;
				left: 50%;
				margin-left: -25px;
				margin-top: -25px;
				height: 50px;
				width: 50px;
			}
			
			.mui-preview-image img.mui-transitioning {
				-webkit-transition: -webkit-transform 0.5s ease, opacity 0.5s ease;
				transition: transform 0.5s ease, opacity 0.5s ease;
			}
			
			@-webkit-keyframes fadeIn {
				0% {
					opacity: 0;
				}
				100% {
					opacity: 1;
				}
			}
			
			@keyframes fadeIn {
				0% {
					opacity: 0;
				}
				100% {
					opacity: 1;
				}
			}
			
			@-webkit-keyframes fadeOut {
				0% {
					opacity: 1;
				}
				100% {
					opacity: 0;
				}
			}
			
			@keyframes fadeOut {
				0% {
					opacity: 1;
				}
				100% {
					opacity: 0;
				}
			}
			
			p img {
				max-width: 100%;
				height: auto;
			}
		</style>
	</head>

	<body contextmenu="return false;">
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 id="toName" class="mui-title">患者</h1>
		</header>
		<pre id='h'></pre>
		<script id='msg-template' type="text/template">
			<% for(var i in record){ var item=record[i]; %>
			<div class="msg-item <%= (item.sender=='self'?' msg-item-self':'') %>" msg-type='<%=(item.type)%>' msg-content='<%=(item.content)%>'>
				<% if(item.sender=='self' ) { %>
				<img class="msg-user msg-user-img" src=""></img>
				<% } else { %>
				<img class="msg-user-img" src="../images/familydoctor/lyy.png" alt="" />
				<!--首先的头像 -->
				<% } %>
				<div class="msg-content">
					<div class="msg-content-inner">
						<% if(item.type=='text' ) { %>
						<%=( item.content|| '&nbsp;&nbsp;') %>
						<% } else if(item.type=='image' ) { %>
						<!--<%=( item.content|| '&nbsp;&nbsp;') %>-->
						<img class="msg-content-image" src="<%=(item.content)%>" style="max-width: 100px;" />
						<% } else if(item.type=='sound' ) { %>
						<span class="mui-icon mui-icon-mic" style="font-size: 18px;font-weight: bold;"></span>
						<span class="play-state">点击播放</span>
						<% } %>
					</div>
					<div class="msg-content-arrow"></div>
				</div>
				<div class="mui-item-clear"></div>
			</div>
			<% } %>
		</script>

		<!--<div id="refreshContainer" class="mui-content">
			<div id="wrapper01" class="wrapper" >
				<ul id="scroller" class="scroller"">-->
					<div class="mui-content">
						<div id='msg-list' class="msgflow">
						</div>
					</div>
				<!--</ul>
			</div>
		</div>-->

		<footer>
			<div class="footer-left">
				<i id='msg-image' class="mui-icon mui-icon-camera" style="font-size: 28px;"></i>
			</div>
			<div class="footer-center">
				<textarea id='msg-text' type="text" class='input-text'></textarea>
				<button id='msg-sound' type="button" class='input-sound' style="display: none;">按住说话</button>
			</div>
			<label for="" class="footer-right">
				<i id='msg-type' class="mui-icon mui-icon-mic"></i>
			</label>
		</footer>
		<div id='sound-alert' class="rprogress">
			<div class="rschedule"></div>
			<div class="r-sigh">!</div>
			<div id="audio_tips" class="rsalert">手指上滑，取消发送</div>
		</div>
		<script src="../js/mui.min.js"></script>
		<script src="../js/mui.previewimage.js"></script>
		<script src="../js/mui.imageViewer.js"></script>
		<script src="../js/mui.zoom.js"></script>

		<script src="../js/arttmpl.js"></script>

		<script type="text/javascript" src="../js/xiangyingshi.js"></script>
		<script type="text/javascript" src="../js/jquery-3.1.1.js"></script>
		<script type="text/javascript" src="../js/sdk/lib/md5/spark-md5.js"></script>
		<script type="text/javascript" src="../js/sdk/lib/md5/md5.min.js"></script>
		<script type="text/javascript" src="../js/constants.js"></script>
		<script type="text/javascript" src="../js/sharemethods.js"></script>
		<script type="text/javascript" src="../js/qs/qs.feedback.js"></script>
		<script type="text/javascript" src="../js/sdk/lib/base64/base64.js"></script>
		<!--描述：展示获取的一条消息-->
		<script type="text/javascript" src="../js/sdk/webim/show_one_msg.js"></script>
		<!--描述：设置聊天对象-->
		<script type="text/javascript" src="../js/sdk/webim/sdk_sendmsg_api.js"></script>
		<!--描述：获取新消息-->
		<script type="text/javascript" src="../js/sdk/webim/receive_new_msg.js"></script>
		<!--描述：聊天登录-->
		<!--描述：发消息-->
		<script type="text/javascript" src="../js/sdk/webim/send_common_msg.js"></script>
		<!--描述：上传图片-->
		<script type="text/javascript" src="../js/sdk/webim/upload_and_send_pic_msg.js"></script>
		<script type="text/javascript" src="../js/sdk/webim/get_history_msg.js"></script>
		<script type="text/javascript" src="../js/sdk/webim.js"></script>
		<script type="text/javascript" src="../js/sdk/webim/login.js"></script>
		<script type="text/javascript" src="../plugs/moment/moment.min.js"></script>
		<!--<script type="text/javascript" src="../js/sdk/webim/recent_contact_list_manager.js"></script>-->
		<script type="text/javascript" src="../js/iscroll/iscroll-probe.js"></script>
		<script type="text/javascript" src="../js/qs/qs.iscroll.js"></script>
		<script type="text/javascript" charset="utf-8">
			mui.previewImage();
			var userImages = null;
			var cuToId = "";
			//聊天窗体
			record = [];
			ui = {
				body: document.querySelector('body'),
				footer: document.querySelector('footer'),
				footerRight: document.querySelector('.footer-right'),
				footerLeft: document.querySelector('.footer-left'),
				btnMsgType: document.querySelector('#msg-type'),
				boxMsgText: document.querySelector('#msg-text'),
				boxMsgSound: document.querySelector('#msg-sound'),
				btnMsgImage: document.querySelector('#msg-image'),
				areaMsgList: document.querySelector('#msg-list'),
				boxSoundAlert: document.querySelector('#sound-alert'),
				h: document.querySelector('#h'),
				content: document.querySelector('.mui-content')
			};

			var imageViewer = new mui.ImageViewer('.msg-content-image', {
				dbl: false
			});
			$(".msg-content-img").addClass("data-preview-src='' data-preview-group='1'");
			var MIN_SOUND_TIME = 800;
			var msgItemTap = function(msgItem, event) {
				var msgType = msgItem.getAttribute('msg-type');
				var msgContent = msgItem.getAttribute('msg-content')
				if(msgType == 'sound') {
					player = plus.audio.createPlayer(msgContent);
					var playState = msgItem.querySelector('.play-state');
					playState.innerText = '正在播放...';
					player.play(function() {
						playState.innerText = '点击播放';
					}, function(e) {
						playState.innerText = '点击播放';
					});
				}
			};
			mui.init({
				keyEventBind: {
					backbutton: true //打开back按键监听
				},
				beforeback: function() {
					localStorage.setItem("cuToId", "");
					cuToId = "";
					plus.webview.getLaunchWebview().evalJS("startListeners()");
					mui.back;
					return true;
				}
			});
			var wrapper01;
			var bindMsgList = function() {
				//绑定数据:
				/*tp.bind({
					template: 'msg-template',
					element: 'msg-list',
					model: record
				});*/

				ui.areaMsgList.innerHTML = template('msg-template', {
					"record": record
				});
				var msgItems = ui.areaMsgList.querySelectorAll('.msg-item');
				[].forEach.call(msgItems, function(item, index) {
					item.addEventListener('tap', function(event) {
						msgItemTap(item, event);
					}, false);
				});
				imageViewer.findAllImage();
				ui.areaMsgList.scrollTop = ui.areaMsgList.scrollHeight + ui.areaMsgList.offsetHeight;
				console.log("userImages===>" + userImages);
				if(userImages) {
					var image_url = serverAddress + '/' + $$.processImagePath(userImages)
					console.log(image_url)
					$(".msg-user").attr("src", image_url)
				}

			};
			mui.init({
				gestureConfig: {
					tap: true, //默认为true
					doubletap: true, //默认为false
					longtap: true, //默认为false
					swipe: true, //默认为true
					drag: true, //默认为true
					hold: true, //默认为false，不监听
					release: true //默认为false，不监听

				}

			});
			template.config('escape', false);
			//$.plusReady=function(fn){fn();};

			mui.plusReady(function() {
				function processConfimAction(myScroll) {}

//				<!--var init = function() {
//					wrapper01 = $.initIscroll({
//						id: "wrapper01",
//						pullUpAction: processConfimAction,
//						pullDownAction: processConfimAction,
//						limit: 15
//					});
//					processConfimAction(wrapper01);
//				}
//				init();-->

				if(plus.navigator.isImmersedStatusbar()) { // 兼容immersed状态栏模式
					// 获取状态栏高度并根据业务需求处理，这里重新计算了子窗口的偏移位置
					topoffset = (Math.round(plus.navigator.getStatusbarHeight()) + 45) + 'px';
					document.querySelector("header").style.height = topoffset;
					document.querySelector("header").style.paddingTop = "20px";
					document.querySelector(".mui-bar-nav~.mui-content").style.paddingTop = topoffset;
				}
				// 加载完毕后关闭等待框，并展示页面
				var currentView = plus.webview.currentWebview();
				currentView.show('slide-in-right', 200);
				plus.nativeUI.closeWaiting();

				userImages = localStorage.getItem("HEAD_PHOTO");

				var to_account = currentView.TO_ACCOUNT;
				var to_name = currentView.TO_NAME
				$("#toName").html(to_name);
				cuToId = to_account;
				chatId = to_account;
				localStorage.setItem("cuToId", to_account);
				//得到聊天记录
				webim.login(webimLoginInfo, listeners, {},
					function(resp) {
						selType = webim.SESSION_TYPE.C2C;
						var getPrePageC2CHistroyMsgInfoMap = {};
						getLastC2CHistoryMsgs(to_account, function(msgList) {
							console.log("得到聊天记录" + msgList.length);
						}, function() {});
					},
					function(err) {}
				);

				plus.webview.currentWebview().setStyle({
					softinputMode: "adjustResize"
				});

				var showKeyboard = function() {
					if(mui.os.ios) {
						var webView = plus.webview.currentWebview().nativeInstanceObject();
						webView.plusCallMethod({
							"setKeyboardDisplayRequiresUserAction": false
						});
					} else {
						var Context = plus.android.importClass("android.content.Context");
						var InputMethodManager = plus.android.importClass("android.view.inputmethod.InputMethodManager");
						var main = plus.android.runtimeMainActivity();
						var imm = main.getSystemService(Context.INPUT_METHOD_SERVICE);
						imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
						//var view = ((ViewGroup)main.findViewById(android.R.id.content)).getChildAt(0);
						imm.showSoftInput(main.getWindow().getDecorView(), InputMethodManager.SHOW_IMPLICIT);
					}
				};

				ui.h.style.width = ui.boxMsgText.offsetWidth + 'px';
				var footerPadding = ui.footer.offsetHeight - ui.boxMsgText.offsetHeight;

				//				bindMsgList();
				window.addEventListener('resize', function() {
					ui.areaMsgList.scrollTop = ui.areaMsgList.scrollHeight + ui.areaMsgList.offsetHeight;
				}, false);
				var send = function(msg) {
					//获取发送的文字，放在聊天框中，发送给指定ID
					//自己发的消息放聊天框中
					//					record.push(msg);
					//如果消息类型是图片
					if(msg.type == "image") {
						onSendMsg(msg.content, to_account, "image", msg.msgObject);
						record.push(msg);
						bindMsgList();
					}
					//如果是文字
					if(msg.type == "text") {
						onSendMsg(msg.content, to_account, "text");
					}
					//					bindMsgList();
					//toRobot(msg.content);
					//console.log(JSON.stringify(onMsgNotify()));
				};
				//				var toRobot = function(info) {
				//					var apiUrl = 'http://www.tuling123.com/openapi/api';
				//					mui.getJSON(apiUrl, {
				//						"key": 'acfbca724ea1b5db96d2eef88ce677dc',
				//						"info": info,
				//						"userid": plus.device.uuid
				//					}, function(data) {
				//						//alert(JSON.stringify(data));
				//						record.push({
				//							sender: 'zs',
				//							type: 'text',
				//							content: data.text
				//						});
				//						bindMsgList();
				//					});
				//				};

				function msgTextFocus() {
					ui.boxMsgText.focus();
					setTimeout(function() {
						ui.boxMsgText.focus();
					}, 150);
				}
				//解决长按“发送”按钮，导致键盘关闭的问题；
				ui.footerRight.addEventListener('touchstart', function(event) {
					if(ui.btnMsgType.classList.contains('mui-icon-paperplane')) {
						msgTextFocus();
						event.preventDefault();
					}
				});
				//解决长按“发送”按钮，导致键盘关闭的问题；
				ui.footerRight.addEventListener('touchmove', function(event) {
					if(ui.btnMsgType.classList.contains('mui-icon-paperplane')) {
						msgTextFocus();
						event.preventDefault();
					}
				});
				ui.footerRight.addEventListener('touchcancel', function(event) {
					if(ui.btnMsgType.classList.contains('mui-icon-paperplane')) {
						msgTextFocus();
						event.preventDefault();
					}
				});
				ui.footerRight.addEventListener('touchend', function(event) {
					if(ui.btnMsgType.classList.contains('mui-icon-paperplane')) {
						msgTextFocus();
						event.preventDefault();
					}
				});
				ui.footerRight.addEventListener('release', function(event) {
					if(ui.btnMsgType.classList.contains('mui-icon-paperplane')) {
						//showKeyboard();
						ui.boxMsgText.focus();
						setTimeout(function() {
							ui.boxMsgText.focus();
						}, 150);
						//							event.detail.gesture.preventDefault();
						send({
							sender: 'self',
							type: 'text',
							content: ui.boxMsgText.value.replace(new RegExp('\n', 'gm'), '<br/>')
						});
						ui.boxMsgText.value = '';
						mui.trigger(ui.boxMsgText, 'input', null);
					} else if(ui.btnMsgType.classList.contains('mui-icon-mic')) {
						ui.btnMsgType.classList.add('mui-icon-compose');
						ui.btnMsgType.classList.remove('mui-icon-mic');
						ui.boxMsgText.style.display = 'none';
						ui.boxMsgSound.style.display = 'block';
						ui.boxMsgText.blur();
						document.body.focus();
					} else if(ui.btnMsgType.classList.contains('mui-icon-compose')) {
						ui.btnMsgType.classList.add('mui-icon-mic');
						ui.btnMsgType.classList.remove('mui-icon-compose');
						ui.boxMsgSound.style.display = 'none';
						ui.boxMsgText.style.display = 'block';
						//--
						//showKeyboard();
						ui.boxMsgText.focus();
						setTimeout(function() {
							ui.boxMsgText.focus();
						}, 150);
					}
				}, false);
				ui.footerLeft.addEventListener('tap', function(event) {
					var btnArray = [{
						title: "拍照"
					}, {
						title: "从相册选择"
					}];
					plus.nativeUI.actionSheet({
						title: "选择照片",
						cancel: "取消",
						buttons: btnArray
					}, function(e) {
						var index = e.index;
						switch(index) {
							case 0:
								break;
							case 1:
								var cmr = plus.camera.getCamera();
								cmr.captureImage(function(path) {
									plus.io.resolveLocalFileSystemURL(path, function(entry) {
										var src = entry.toLocalURL();
										//resizeImage(src)//进行压缩
										entry.file(function(file) {
											send({
												sender: 'self',
												type: 'image',
												content: src,
												msgObject: file.size
											}); /*上传图片*/
										})
									}, function(e) {
										console.log(e.message);
									});
								}, function(err) {});
								break;
							case 2:
								plus.gallery.pick(function(path) {
									plus.io.resolveLocalFileSystemURL(path, function(entry) {
										entry.file(function(file) {
											send({
												sender: 'self',
												type: 'image',
												content: path,
												msgObject: file.size
											}); /*上传图片*/
										})
									}, function(e) {
										//										alert("Resolve file URL failed: " + e.message);
									});
								}, function(err) {}, null);
								break;
						}
					});
				}, false);
				var setSoundAlertVisable = function(show) {
					if(show) {
						ui.boxSoundAlert.style.display = 'block';
						ui.boxSoundAlert.style.opacity = 1;
					} else {
						ui.boxSoundAlert.style.opacity = 0;
						//fadeOut 完成再真正隐藏
						setTimeout(function() {
							ui.boxSoundAlert.style.display = 'none';
						}, 200);
					}
				};
				var recordCancel = false;
				var recorder = null;
				var audio_tips = document.getElementById("audio_tips");
				var startTimestamp = null;
				var stopTimestamp = null;
				var stopTimer = null;
				ui.boxMsgSound.addEventListener('hold', function(event) {
					recordCancel = false;
					if(stopTimer) clearTimeout(stopTimer);
					audio_tips.innerHTML = "手指上划，取消发送";
					ui.boxSoundAlert.classList.remove('rprogress-sigh');
					setSoundAlertVisable(true);
					recorder = plus.audio.getRecorder();
					if(recorder == null) {
						plus.nativeUI.toast("不能获取录音对象");
						return;
					}
					startTimestamp = (new Date()).getTime();
					recorder.record({
						filename: "_doc/audio/"
					}, function(path) {
						if(recordCancel) return;
						send({
							sender: 'self',
							type: 'sound',
							content: path
						});
					}, function(e) {
						plus.nativeUI.toast("录音时出现异常: " + e.message);
					});
				}, false);
				ui.body.addEventListener('drag', function(event) {
					//console.log('drag');
					if(Math.abs(event.detail.deltaY) > 50) {
						if(!recordCancel) {
							recordCancel = true;
							if(!audio_tips.classList.contains("cancel")) {
								audio_tips.classList.add("cancel");
							}
							audio_tips.innerHTML = "松开手指，取消发送";
						}
					} else {
						if(recordCancel) {
							recordCancel = false;
							if(audio_tips.classList.contains("cancel")) {
								audio_tips.classList.remove("cancel");
							}
							audio_tips.innerHTML = "手指上划，取消发送";
						}
					}
				}, false);
				ui.boxMsgSound.addEventListener('release', function(event) {
					//console.log('release');
					if(audio_tips.classList.contains("cancel")) {
						audio_tips.classList.remove("cancel");
						audio_tips.innerHTML = "手指上划，取消发送";
					}
					//
					stopTimestamp = (new Date()).getTime();
					if(stopTimestamp - startTimestamp < MIN_SOUND_TIME) {
						audio_tips.innerHTML = "录音时间太短";
						ui.boxSoundAlert.classList.add('rprogress-sigh');
						recordCancel = true;
						stopTimer = setTimeout(function() {
							setSoundAlertVisable(false);
						}, 800);
					} else {
						setSoundAlertVisable(false);
					}
					recorder.stop();
				}, false);
				ui.boxMsgSound.addEventListener("touchstart", function(e) {
					//console.log("start....");
					e.preventDefault();
				});
				ui.boxMsgText.addEventListener('input', function(event) {
					ui.btnMsgType.classList[ui.boxMsgText.value == '' ? 'remove' : 'add']('mui-icon-paperplane');
					ui.btnMsgType.setAttribute("for", ui.boxMsgText.value == '' ? '' : 'msg-text');
					ui.h.innerText = ui.boxMsgText.value.replace(new RegExp('\n', 'gm'), '\n-') || '-';
					ui.footer.style.height = (ui.h.offsetHeight + footerPadding) + 'px';
					ui.content.style.paddingBottom = ui.footer.style.height;
				});
				var focus = false;
				ui.boxMsgText.addEventListener('tap', function(event) {
					ui.boxMsgText.focus();
					setTimeout(function() {
						ui.boxMsgText.focus();
					}, 0);
					focus = true;
					setTimeout(function() {
						focus = false;
					}, 1000);
					event.detail.gesture.preventDefault();
				}, false);
				//点击消息列表，关闭键盘
				ui.areaMsgList.addEventListener('click', function(event) {
					if(!focus) {
						ui.boxMsgText.blur();
					}
				});
			});
		</script>

	</body>

</html>